package com.chu.controller;

import com.chu.entity.Dept;
import com.chu.entity.Menu;
import com.chu.entity.Role;
import com.chu.enums.PageEnum;
import com.chu.service.DeptService;
import com.chu.service.MenuService;
import com.chu.service.RoleService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class RoleController {


    @Autowired
    private RoleService roleService;

    @Autowired
    private DeptService deptService;

    @Autowired
    private MenuService menuService;

    //修改角色信息的回显
    @RequestMapping("/resource/demo3/findByRoleId")
    public String findByRoleId(int roleid,ModelMap map){
        //角色信息
        Role role = roleService.selectByPrimaryKey(roleid);
        map.addAttribute("role",role);
        for (Integer integer : role.getMenuIdList()) {
            System.out.println(integer);
        }
        //部门信息
        List<Dept> dept = deptService.findAllDept();
        map.addAttribute("dept",dept);

        //所有权限信息
        List<Menu> menus = menuService.findAllMenus();
        map.addAttribute("menus",menus);

        return "/resource/demo3/update";


    }


    //分页查询所有角色信息
    @RequestMapping("/resource/demo3/jobInstall")
    @RequiresPermissions("user:jobInstall")
    public String jobInstall(@RequestParam(defaultValue = "1")int page, ModelMap map, String rname, String deptname, String  state){

        PageInfo<Role> pageInfo =
                roleService.findAllOrgRole( page, PageEnum.SIZE.getSize(), rname, deptname, state);
        map.addAttribute("pageInfo",pageInfo);
        map.addAttribute("rname",rname);
        map.addAttribute("deptname",deptname);
        map.addAttribute("state",state);

        return "resource/demo3/list";
    }

    //增加Role的数据回显
    @RequestMapping("/resource/demo3/getDept")
    public String getDept(ModelMap map){

        System.out.println("进入增加role的数据回显");

        List<Dept> deptList = deptService.findAllDept();
        map.addAttribute("deptList",deptList);
        System.out.println("查询所有部门");

        List<Menu> menuList = menuService.findAllMenus();
        map.addAttribute("menuList",menuList);
        System.out.println("查询所有权限");

        return "/resource/demo3/add";
    }

    //增加角色信息
    @RequestMapping("/resource/demo3/addRole")
    public String addRole(Role role,int[] mid){
        roleService.insert(role,mid);
        return "redirect:/resource/demo3/jobInstall";
    }

    //根据部门id查询所有的角色
    @RequestMapping("/getRole")
    @ResponseBody
    public List<Role> getRole(int deptId){

        List<Role> roleList = roleService.findAllRole(deptId);

        return roleList;
    }


}
